package com.sinochem.yunlian.ship.base.rest.app;

import com.alibaba.fastjson.JSONObject;
import com.sinochem.yunlian.ship.base.biz.BsPortBiz;
import com.sinochem.yunlian.ship.base.constants.BsPortConstant;
import com.sinochem.yunlian.ship.base.entity.BsPort;
import com.sinochem.yunlian.ship.base.vo.response.PortVo;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import com.sinochem.yunlian.ship.common.constant.RedisKey;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author panyanan
 * @date 2017-11-20 21:02
 *
 * */
@IgnoreToken
@RestController
@RequestMapping("/m/port")
public class MBsPortController extends BaseController<BsPortBiz,BsPort> {


    @Autowired
    private BsPortBiz bsPortBiz;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查找港口
     * */
    @RequestMapping(value = "/findAll/anon", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse findAllPorts(){
        RestResponse result = new RestResponse();
        List<PortVo> portVos = new ArrayList<PortVo>();
        if (redisTemplate.hasKey(RedisKey.KEY_PORT_ALL) ) {
            String portsStr = (String) redisTemplate.opsForValue().get(RedisKey.KEY_PORT_ALL);
            portVos = JSONObject.parseArray(portsStr,PortVo.class);
        } else {
            //查询所有已经启用的港口信息
            List<BsPort> ports = bsPortBiz.findAllPorts();

            for (BsPort port : ports) {
                if (port.getParentId() == 0 && port.getLevel() == 1) {
                    PortVo vo = transformEntity2Vo(port);
                    for (BsPort portOne : ports) {
                        if (portOne.getLevel() == 2 && portOne.getParentId().equals(vo.getId())) {
                            PortVo voOne = transformEntity2Vo(portOne);
                            if (vo.getChildrens() == null) {
                                vo.setChildrens(new ArrayList<>());
                            }
                            for (BsPort portTwo : ports) {
                                PortVo voTwo = transformEntity2Vo(portTwo);
                                if (portTwo.getLevel() == 3 && portTwo.getParentId().equals(portOne.getId())) {
                                    if (voOne.getChildrens() == null) {
                                        voOne.setChildrens(new ArrayList<>());
                                    }
                                    voOne.getChildrens().add(voTwo);
                                }
                            }
                            if(voOne.getChildrens() != null && voOne.getChildrens().size() != 0){
                                vo.getChildrens().add(voOne);
                            }
                        }
                    }
                    if(vo.getChildrens() !=null && vo.getChildrens().size() != 0){
                        portVos.add(vo);
                    }
                }
            }
            redisTemplate.opsForValue().set(RedisKey.KEY_PORT_ALL,JSONObject.toJSONString(portVos));
        }
        result.setData(portVos);
        return result;
    }



    public PortVo transformEntity2Vo (BsPort bsPort) {
        PortVo vo = new PortVo();
        vo.setId(bsPort.getId());
        vo.setPortName(bsPort.getPortName());
        vo.setParentId(bsPort.getParentId());
        vo.setLat(bsPort.getLat());
        vo.setLon(bsPort.getLon());
        vo.setLevel(bsPort.getLevel());
        vo.setPortCode(bsPort.getPortCode());
        vo.setShortName(bsPort.getShortName());
        return vo;
    }


    /**
     * 查找港口
     * */
    @RequestMapping(value = "/findAllPorts", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse findAllPortsByLevel(){
        RestResponse result = new RestResponse();
        List<PortVo> portVos = new ArrayList<PortVo>();
        //查询所有已启用级别为3级的港口信息
        List<BsPort> ports = bsPortBiz.findAllPortsByLevel(BsPortConstant.STATUS_ENABLE,BsPortConstant.LEVLE_3);
        for (BsPort port : ports) {
             PortVo vo = transformEntity2Vo(port);
             portVos.add(vo);
        }
        result.setData(portVos);
        return result;
    }

}